// appUserRoute-用户模块相关接口
const {
	SuccessModel,
	ErrorModel,
  AuthErrorModel
} = require("../../model/responseModel");

const { 
  appLoginCheck, 
  loginFun, 
  siginUpFun,
  getAppUserFun
} = require('../../controllers/appUser.controller');
const { md5 } = require('../../extend/utils');

const appUserRoute = async (req, res) => {
	const method = req.method;
	const bodyData = req.body;
	const headers = req.headers;

  if (method === 'GET' && req.path === '/') {
    return new SuccessModel({
      simple_home_api: 'success'
    });
  }

  /**
   * * 用户端登录
  */
  if (method === 'POST' && req.path === '/app/v1/api/login') {
    try {
      const username =  bodyData.username || '';
      const password = md5(bodyData.password) || '';
      const loginPromise = loginFun(username, password);
      return loginPromise.then(bodyData => {
        if (bodyData) {
          return new SuccessModel(bodyData);
        } else {
          return new ErrorModel('登录失败,请检查账号密码');
        }
      });
    } catch(e) {
      return new ErrorModel('登录失败,请检查账号密码');
    }
  }

  /**
   * * 用户端注册
  */
  if (method === 'POST' && req.path === '/app/v1/api/signUp') {
    try {
      // 判断数据完整性
      if (!bodyData.phone || !bodyData.password || !bodyData.userName) {
        return new ErrorModel('参数错误');
      }
      // 判断手机号是否已存在
      const user = await getAppUserFun('phone', bodyData.phone);
      if (user && user.length) {
        return new ErrorModel('手机号已被注册');
      } else {
        // 注册账号
        const siginUp = await siginUpFun(bodyData);
        console.log("siginUp => ", siginUp);
        if (siginUp && siginUp.affectedRows === 1) {
          return new SuccessModel('注册成功');
        }
      }
    } catch(e) {
      console.log("e => ", e);
      return new ErrorModel('注册失败');
    }
  }

  /**
   * * 查询用户详情
  */
  if (method === 'POST' && req.path === '/app/v1/api/userInfo') {
    try {
      // 判断登录状态
      const userCheck = await appLoginCheck(headers['user-agent']);
      if (userCheck && userCheck.length) {
        const uid = bodyData.id || '';
        const userInfo = await getAppUserFun('id', uid);
        if (userInfo) {
          return new SuccessModel(userInfo);
        } else {
          return new ErrorModel('用户不存在');
        }
      } else {
        return new AuthErrorModel();
      }
    } catch(e) {
      return new ErrorModel('获取失败');
    }
  }

  // test----------------------------------------------------------------

  /**
   * * 查询用户订单
  */
  if (method === 'POST' && req.path === '/app/v1/api/orderList') {
    try {
      // 判断登录状态
      const userCheck = await appLoginCheck(headers['user-agent']);
      if (userCheck && userCheck.length) {
        const uid = bodyData.id || '';
        const userInfo = await getAppUserFun('id', uid);
        if (userInfo) {
          const orderList = await getOrderListFun('id', uid);
          return new SuccessModel(orderList);
        } else {
          return new ErrorModel('用户不存在');
        }
      } else {
        return new AuthErrorModel();
      }
    } catch(e) {
      return new ErrorModel('获取失败');
    }
  }

  /**
   * * 订单状态流转
  */
  if (method === 'POST' && req.path === '/app/v1/api/orderStatusEdit') {
    try {
      // 判断登录状态
      const userCheck = await appLoginCheck(headers['user-agent']);
      if (userCheck && userCheck.length) {
        const uid = bodyData.id || '';
        const orderId = bodyData.orderId || '';
        const status = bodyData.status || '';
        const userInfo = await getAppUserFun('id', uid);
        const orderInfo = await getAppOrderFun('id', orderId);
        if (userInfo && orderInfo) {
          await orderStatusEditFun(orderId, status);
          return new SuccessModel("修改成功");
        } else {
          return new ErrorModel('修改失败');
        }
      } else {
        return new AuthErrorModel();
      }
    } catch(e) {
      return new ErrorModel('修改失败');
    }
  }

  /**
   * * 编辑个人信息
  */
  if (method === 'POST' && req.path === '/app/v1/api/setUserInfo') {
    try {
      const userCheck = await appLoginCheck(headers['user-agent']);
      if (userCheck && userCheck.length) {
        const uid = bodyData.id || '';
        const userInfo = await getAppUserFun('id', uid);
        if (userInfo) {
          await setUserInfoFun(uid, bodyData);
          return new SuccessModel("修改成功");
        } else {
          return new ErrorModel('修改失败');
        }
      } else {
        return new AuthErrorModel();
      }
    } catch(e) {
      return new ErrorModel('修改失败');
    }
  }

  /**
   * * 收货地址列表
  */
  if (method === 'POST' && req.path === '/app/v1/api/userAddressList') {
    try {
      const userCheck = await appLoginCheck(headers['user-agent']);
      if (userCheck && userCheck.length) {
        const uid = bodyData.id || '';
        const AddressList = await getUserAddressListFun('id', uid);
        if (userInfo) {
          return new SuccessModel(AddressList);
        } else {
          return new ErrorModel('用户不存在');
        }
      } else {
        return new AuthErrorModel();
      }
    } catch(e) {
      return new ErrorModel('获取失败');
    }
  }

  
}

module.exports = appUserRoute;
